# -*- coding: utf-8 -*-

#    OriginDB
#    Copyright (C) 2009  Jason Power <power.jg@gmail.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

from dbobject import DBObject
from chemdb import dbHandle

class Reference(DBObject):

	def __init__(self, referenceId=-1):

		DBObject.__init__(self)

		self.id = referenceId

		#string
		self._things['title'] = ""
		self._things['author_name'] = ""
		self._things['source'] = ""
		self._things['abstract'] = ""

		#int
		self._things['volume'] = 0
		self._things['issue'] = 0
		self._things['year'] = 1800
		self._things['pageStart'] = 0
		self._things['pageEnd'] = 0

		print self.id


	def _getItem(self, name):
		if self.id == -1:
			return self._things[name]
		if name=="author_name":
			author = dbHandle.get('author_name', 'reference', 'id', self.id)
			self._things['author_name'] = author
			return author
		if name == "year":
			year = dbHandle.get('year', 'reference', 'id', self.id)
			self._things['year'] = int(year)
			return year
		if name=="title":
			title = dbHandle.get('title', 'reference', 'id', self.id)
			self._things['title'] = title
			return title
		if name=="source":
			source = dbHandle.get('source', 'reference', 'id', self.id)
			self._things['source'] = source
			return source
		if name=="volume":
			volume = dbHandle.get('volume', 'reference', 'id', self.id)
			self._things['volume'] = int(volume)
			return volume
		if name=="issue":
			issue = dbHandle.get('issue', 'reference', 'id', self.id)
			self._things['issue'] = int(issue)
			return issue
		if name=="abstract":
			abstract = dbHandle.get('abstract', 'reference', 'id', self.id)
			self._things['abstract'] = abstract
			return abstract
		if name=="pageStart":
			pageStart = dbHandle.get('page_start', 'reference', 'id', self.id)
			self._things['pageStart'] = int(pageStart)
			return pageStart
		if name=="pageEnd":
			pageEnd = dbHandle.get('page_end', 'reference', 'id', self.id)
			self._things['pageEnd'] = int(pageEnd)
			return pageEnd

	def insert(self):
		if self.id != -1:
			raise AttributeError, "Object already in DB"

		self.id = dbHandle.insertReference()

		for i in self._needToUpdate:
			self.updateDB(i)
		self._needToUpdate = []

	def updateDB(self, name):
		if self.id == -1:
			self._needToUpdate.append(name)
			return

		if name in self._things.keys():
			if name=="pageStart":
				dbHandle.updateReference(self.id, 'page_start', self._things[name])
			elif name=="pageEnd":
				dbHandle.updateReference(self.id, 'page_end', self._things[name])
			else:
				dbHandle.updateReference(self.id, name, self._things[name])
		else:
			raise NameError, name

def searchForReference(reference):

	query = {}
	if reference.title:
		query['title'] = reference.title
	#if reference.source:
		#query['source'] = reference.source
	#if reference.author_name:
		#query['author_name'] = reference.author_name
	#if reference.volume:
		#query['volume'] = reference.volume

	ids = dbHandle.searchLike(query, 'reference')

	return [Reference(i[0]) for i in ids]